module.exports = {
	back(delta){ uni.navigateBack({delta:delta||1})},
	navto(url){
		uni.navigateTo({
			url:url||'/pages/index/index',
			fail(){uni.switchTab({url,fail(err){console.log(err);}})}
		})
	},
	redirect(url){ uni.redirectTo({url:url||'/pages/index/index',fail(err){console.log(err);}}) },
	relaunch(url){ uni.reLaunch({url:url||'/pages/index/index',fail(err){console.log(err);}}) },
	index(){this.reLaunch('/pages/index/index')},
	
	
	showLoading(title='加载中',mask=1){ uni.showLoading({title,mask}) },
	hideLoading(){ uni.hideLoading() },
	tip(content,isMask=false,cb,duration=1){
		uni.showToast({
			title: content||'提示内容',
			icon: 'none',
			mask: isMask,
			success() {
				if(duration > 0){
					setTimeout(()=>{
						cb && cb()
					}, duration * 1000)
				}else{
					cb && cb()
				}
			}
		})
	},
	success(content,isMask=false,cb,duration=1){
		uni.showToast({
			title: content||'提示内容',
			icon: 'success',
			mask: isMask,
			success() {
				if(duration > 0){
					setTimeout(()=>{
						cb && cb()
					}, duration * 1000)
				}else{
					cb && cb()
				}
			}
		})
	},
	error(content,isMask=false,cb,duration=1){
		uni.showToast({
			title: content||'提示内容',
			icon: 'error',
			mask: isMask,
			success() {
				if(duration > 0){
					setTimeout(()=>{
						cb && cb()
					}, duration * 1000)
				}else{
					cb && cb()
				}
			}
		})
	},
	alert(content,cb){
		uni.showModal({
			title: '提示',
			content:content||'提示内容',
			showCancel: false,
			success() {
				cb && cb()
			}
		})
	},
	confirm(content,cb1,cb0,opts={}){
		uni.showModal({
			title: '确认',
			content: content||'确认内容',
			cancelText: opts.cancelText || '取消',
			confirmText: opts.confirmText || '确定',
			success(res) {
				if(res.confirm) cb1 && cb1()
				else cb0 && cb0()
			}
		})
	},
	cache(key, value=null){
		if(value === null){
			let data = uni.getStorageSync(key);
			return data;
		}
		else return uni.setStorageSync(key, value)
	},
	removeCache(key){
		return uni.removeStorageSync(key)
	},
	clearCache(key){
		return uni.clearStorageSync()
	},
	setNavTitle(title) {
		uni.setNavigationBarTitle({
			title
		})
	},
	/**
	 * 去抖 将多次执行变为最后一次执行
	 * @desc 函数防抖---“立即执行版本” 和 “非立即执行版本” 的组合版本
	 * @param func 需要执行的函数
	 * @param wait 延迟执行时间（毫秒）
	 * @param immediate---true 表立即执行，false 表非立即执行
	 **/
	debounce(func, wait = 1000, immediate = true) {
		let timer;
		return function() {
			let context = this;
			let args = arguments;

			if (timer) clearTimeout(timer);
			if (immediate) {
				let callNow = !timer;
				timer = setTimeout(() => {
					timer = null;
				}, wait);
				if (callNow) func.apply(context, args)
			} else {
				timer = setTimeout(function() {
					func.apply(context, args)
				}, wait);
			}
		}
	},

	/**
	 * 节流 将多次执行变为每隔一段时间执行
	 * 当持续触发事件时，保证在一定时间内只调用一次事件处理函数
	 */
	throttle(func, wait = 1000) {
		let prev = Date.now();
		return function() {
			let now = Date.now();
			let that = this;
			if (now - prev >= wait) {
				func.apply(that, arguments);
				prev = Date.now();
			}
		}
	},
	
	// 得到当前页
	getCurrPage(){
		let pages = getCurrentPages();
		let length = pages.length;
		if(length < 1) return null;
		return pages[pages.length-1];
	},
	// 得到上一页
	getPrevPage(){
		let pages = getCurrentPages();
		let length = pages.length;
		if(length <= 1) return null;
		return pages[length-2];
	},
	
	sec2str(sec){
		// let sec = value;//秒
		let middle = 0;//分
		let hour = 0;//小时
		if (sec > 59){
		  middle = parseInt(sec / 60);
		  sec = parseInt(sec % 60);
		}
		if (middle > 59) {
		  hour = parseInt(middle / 60);
		  middle = parseInt(middle % 60);
		}
		sec < 10 ? sec = '0' + sec : sec = sec
		middle < 10 ? middle = '0' + middle : middle = middle
		hour < 10 ? hour = '0' + hour : hour = hour
		return hour + ':' + middle + ':' + sec
	},
	
	secondsToHms(seconds) {
	  const h = Math.floor(seconds / 3600);
	  const m = Math.floor(seconds % 3600 / 60);
	  const s = Math.floor(seconds % 60);
	
	  const hDisplay = h > 0 ? (h<10?'0'+h:h) + "时" : "";
	  const mDisplay = m > 0 ? (m<10?'0'+m:m) + "分" : "";
	  const sDisplay = s > 0 ? (s<10?'0'+s:s) + "秒" : "";
	
	  return hDisplay + mDisplay + sDisplay;
	},
	isPhone(phone){
		return /^1[3-9][0-9]{9}$/.test(phone);
	},
	isNumber(val){
		if(typeof val == "number") return true;
		var regPos = /^[0-9]+.?[0-9]*/; //判断是否是数字。
		return regPos.test(val);
	},
	format(timestamp) {
		var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
		var Y = date.getFullYear() + '-';
		var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1):date.getMonth()+1) + '-';
		var D = (date.getDate()< 10 ? '0'+date.getDate():date.getDate())+ ' ';
		var h = (date.getHours() < 10 ? '0'+date.getHours():date.getHours())+ ':';
		var m = (date.getMinutes() < 10 ? '0'+date.getMinutes():date.getMinutes()) + ':';
		var s = date.getSeconds() < 10 ? '0'+date.getSeconds():date.getSeconds();
		return Y+M+D+h+m+s;
	},
	uuid() {
		var d = new Date().getTime();
		var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
		  var r = (d + Math.random()*16)%16 | 0;
		  d = Math.floor(d/16);
		  return (c=='x' ? r : (r&0x3|0x8)).toString(16);
		});
		return uuid;
	},
	format(timestamp) {
		let i = 1;
		if(timestamp <= 9999999999) i = 1000; //时间戳为10位需*1000，时间戳为13位的话不需乘1000
		var date = new Date(timestamp * i);
		var Y = date.getFullYear() + '-';
		var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1):date.getMonth()+1) + '-';
		var D = (date.getDate()< 10 ? '0'+date.getDate():date.getDate())+ ' ';
		var h = (date.getHours() < 10 ? '0'+date.getHours():date.getHours())+ ':';
		var m = (date.getMinutes() < 10 ? '0'+date.getMinutes():date.getMinutes()) + ':';
		var s = date.getSeconds() < 10 ? '0'+date.getSeconds():date.getSeconds();
		return Y+M+D+h+m+s;
	}
}